package com.lottery.ruok.lottrey.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.lottery.ruok.lottrey.R;
import com.lottery.ruok.lottrey.eventbusObject.ForgetPassword;
import com.lottery.ruok.lottrey.retrofitModule.RetrofitBase;
import com.lottery.ruok.lottrey.retrofitModule.RxHelper;
import com.lottery.ruok.lottrey.retrofitModule.RxSubscribe;
import com.lottery.ruok.lottrey.retrofitModule.request.ForgetPasswordRequest;
import com.lottery.ruok.lottrey.retrofitModule.response.ForgetPasswordResponse;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import ui.ItemPasswordLayout;
import utils.Utils;

public class ForgetPasswordActivity2 extends BaseActivity {


    @BindView(R.id.password)
    ItemPasswordLayout password;
    @BindView(R.id.forget)
    TextView forget;
    @BindView(R.id.next)
    TextView next;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forget_password2);
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);

        init();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void finishSelf(ForgetPassword forgetPassword){
        finish();
    }

    private void init() {
        password.setInputCompleteListener(new ItemPasswordLayout.InputCompleteListener() {
            @Override
            public void inputComplete() {
                next.setEnabled(true);
                next.setTextColor(Color.parseColor("#ffffff"));
            }

            @Override
            public void inputing() {
                next.setEnabled(false);
                next.setTextColor(Color.parseColor("#8539d1"));
            }
        });
    }

    @OnClick({R.id.forget, R.id.next})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.forget:
                Utils.showToast("请联系客服找回交易密码");
                break;
            case R.id.next:
                //sendAnalyticMsg("lottery_" + LoginActivity.ANALYTIC_VERSION + "_me_setting_forgetpwd_deal_next");

                final String phones = getIntent().getStringExtra("phone");// 小韓在 step 1 返回的 "e164String_phone" 格式
                RetrofitBase.getRetrofit().create(ForgetPasswordRequest.class)
                        .getResult(2, phones, password.getStrPassword(), null, null)
                        .compose(RxHelper.<ForgetPasswordResponse>handleResult())
                        .subscribe(new RxSubscribe<ForgetPasswordResponse>(this) {
                            @Override
                            protected void _onNext(ForgetPasswordResponse forgetPasswordResponse) {
                                if(forgetPasswordResponse.getStatus() == 1){
                                    Intent intent = new Intent(ForgetPasswordActivity2.this, ForgetPasswordActivity3.class);
                                    intent.putExtra("phone", phones);
                                    intent.putExtra("e164String", getIntent().getStringExtra("e164String"));
                                    startActivity(intent);
                                }
                            }

                            @Override
                            protected boolean showDialog() {
                                return true;
                            }
                        });
                break;
        }
    }
}
